<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Eugene
 * @Date: 2023-06-20 19:55:03
 * @LastEditors 肖宇 953675853@qq.com
 * @LastEditTime 2024-09-11 15:36:17
 */

namespace app\admin\controller\tj;

use app\common\controller\Backend;
use app\common\model\shop\Ordergoods;
use think\Db;

/**
 * 业绩排行
 *
 * @icon fa fa-circle-o
 */
class Grade extends Backend
{

    /**
     * Order模型对象
     * @var \app\common\model\shop\Order
     */
    protected $model = null;
    protected $acctLimit = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\shop\Order;
    }



    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // $this->model->where('status', '>', '0');
            if($this->auth->acct_id){
                $this->model->where('order.acct_id',$this->auth->acct_id);
            }
            $this->model->with('user')->where('order.status', '>', '0')->group('order.user_id')->field('sum(order.total_fee) as num')->orderRaw('num desc');
            $filter = json_decode($this->request->get('filter'), 1);
            if ($filter && array_key_exists('time_range',$filter)) {
                // var_dump($filter);die;

                $arr = explode(' - ', $filter['time_range']);
                $this->model->where('order.createtime', 'between', [strtotime($arr[0]), strtotime($arr[1] . ' 23:59:59')]);
            }
  

            if ($filter && array_key_exists('acct_id', $filter)) {
                $this->model->where('order.acct_id', $filter['acct_id']);
            }
            $list = $this->model->paginate();
            foreach ($list as $k => $row) {
                           
                // $row->getRelation('custom')->visible(['name']);
				$row->getRelation('user')->visible(['nickname']);
                $row->visible(['num', 'user_id','user','id']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());


            return json($result);
        }
        return $this->view->fetch();
    }
}
